/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: message.h
 *
 * Purpose: message base define
 *
 * Developer:
 *   wen.gu , 2021-05-10
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __MESSAGE_H__
#define __MESSAGE_H__

#include <stdint.h>
#include <vector>
#include <memory>

#include "type_defs.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/


enum class MessageType: uint8_t
{
    kEvent = 0x0, /** event message (service -> proxy) */
    kEventSubscribe = 0x01, /** proxy subscirbe a event from service (proxy -> service) */
    kEventSubscribeAck = 0x02, /** return the subscribe state (service -> proxy) */
    kEventUnsubscribe = 0x03, /** unsubscribe a event (proxy -> service) */
    kEventUnsubscribeAck = 0x04, /** return unsubscribe state (service -> proxy */

    kFieldGet = 0x11,     /** get field value from service (proxy -> service) */
    kFieldGetAck = 0x12,  /** return got value from service field (service - > proxy) */
    kFieldSet = 0x13,    /** set a value to service field (proxy -> service) */
    kFieldSetAck = 0x14, /** return the new value after field set (service - > proxy) */
    kFieldNotify = 0x15, /** notify field changed value (service -> proxy) */ 
    kRequest = 0x20,  /** request a method call (proxy -> service) */
    kRequestOneShot = 0x21, /** request a method without response (proxy -> service) */
    kResponse = 0x22, /** return the result of after method call (service -> proxy) */  

    kError = 0x91, /** error from service to proxy (service -> proxy) */    
};

/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
class Message
{
public:
    using Payload = std::vector<uint8_t>;
    using PayloadPtr = std::shared_ptr<Payload>;
public:
    Message(MessageType type)
    :type_(type)
    {
        /** todo something */
    }
    virtual ~Message(){}

public:
    virtual MessageType type() const { return type_;}
    virtual ClientId client_id() const { return client_id_;}
    virtual SessionId session_id() const {return session_id_;}
    virtual MessageId message_id() const {return message_id_;}
    virtual ErrorCode error_code() const {return error_code_;}
    virtual PayloadPtr payload() const {return payload_;}

    virtual void set_type(MessageType type) {type_ = type;}
    virtual void set_client_id(ClientId id) { client_id_ = id;}
    virtual void set_session_id(SessionId id) {session_id_ = id;}
    virtual void set_message_id(MessageId id) {message_id_ = id;}
    virtual void set_error_code(ErrorCode code) {error_code_ = code;}
    virtual void set_payload(PayloadPtr payload) {payload_ = payload;}
public:
    virtual uint32_t header_size() const {return sizeof(session_id_) + sizeof(type_) + sizeof(message_id_) + sizeof(error_code_); }
    virtual uint32_t payload_size()  const { return (payload_ != nullptr) ? payload_->size(): 0;}
    virtual uint32_t message_size() const {return header_size() + payload_size();}
protected:
    /** if service endian, indicate that where is the message come from */
    MessageType type_;
    ErrorCode error_code_ = ErrorCode::OK;
    ClientId client_id_  = 0;
    SessionId session_id_ = 0; /**like packet id, a unique sequence */
    MessageId message_id_ = 0;
    PayloadPtr payload_ = nullptr;
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
using MessagePtr = std::shared_ptr<Message>;

#endif /** !__MESSAGE_H__ */

